<?php
	session_start();
include_once '../pushing.php';

if ($pushTime == true) {
	if ($_SESSION['UserId'] != 3)
		header('Location: push.php');
}

	if(!isset($_SESSION['UserId']))
	{
		header('Location: login.php');
   		die();
	}
	
	require_once '../mysqlConnect.php'; //mysqli database connect

	function insertUser($patID, $diagnoseDate, $dob, $gender, $relationship, $occupation, $workEnv, $huntEnv, $playEnv, $gardenEnv, $tick, $rash)
	{ // inserts user information into database
		$conn = GetConnection();
		$sql = "INSERT INTO Patient (PatID, DOB, Gender, RelationshipStatus, OccupationStatus, WorkHabitat, 
		HuntHabitat, PlayHabitat, GardenHabitat, DateDiagnosed, TickBite, Rash) 
		VALUES ('$patID', '$dob', '$gender', '$relationship', '$occupation', '$workEnv', '$huntEnv', '$playEnv', 
		 '$gardenEnv', '$diagnoseDate', '$tick', '$rash')";
		$value = $conn->query($sql);
		$conn->close();
	}
	if (isset($_REQUEST['isPostBack'])) // checks to see that hidden variable in form has been set to true
	// (meaning that the user has submitted the form)
	{		
		$ValidationErrors = array();
		
		if ($_REQUEST['patientIDField'] == null || $_REQUEST['patientIDField'] == '') {
			$ValidationErrors[] = array('patientIDdivId', 'PatientID Not Specified');
		}
		if ($_REQUEST['patientDiagnoseDate'] == null || $_REQUEST['patientDiagnoseDate']  == '') {
			$ValidationErrors[] = array('diagnoseIddivId', 'Diagnosis Date Not Specified');
		}
		if ($_REQUEST['patientDOB'] == null || $_REQUEST['patientDOB'] == '') {
			$ValidationErrors[] = array('dobSpanId', 'Date of Birth Not Specified');
		}
		if (count($ValidationErrors) < 1)
		{// send information to function insertUser above to upload to database - creates new patient
			
			
			insertUser($_REQUEST['patientIDField'], $_REQUEST['patientDiagnoseDate'], $_REQUEST['patientDOB'], 
			 $_REQUEST['patientGender'], $_REQUEST['patientRelations'], $_REQUEST['patientOccupation'], $_REQUEST['patientWorkEnv'],
			 $_REQUEST['patientHuntEnv'], $_REQUEST['patientPlayEnv'], $_REQUEST['patientGardenEnv'], $_REQUEST['patientTick'], $_REQUEST['patientRash']);
			 
			 // show an alert to the user that they have successfully uploaded to the database
			 // and redirect to the home page(home.php)
			 echo '<script language="javascript" type="text/javascript">
				alert("Patient ' . $_REQUEST['patientIDField'] . ' Has Been Added to the Database");
				redirect();
				function redirect(){
					window.location.href = "home.php";
				}
				</script>';
		}
}
?>

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Patient</title>
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="../JavaScript/jQueryTabs/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../JavaScript/jQueryTabs/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../Styler.css"/>
<link rel="stylesheet" type="text/css" href="../JavaScript/jQueryTabs/css/custom-theme/jquery-ui-1.8.16.custom.css"/>
<script type="text/javascript">
$(document).ready(function(){

	$("#diagnoseDate").datepicker({ minDate: new Date(1950, 01, 01), showOn: 'both', dateFormat: 'yy-mm-dd'});
	$("#patDOB").datepicker({ minDate: new Date(1950, 01, 01), dateFormat: 'yy-mm-dd'});

	
	<?php 
		if (isset($ValidationErrors)) {	
			if (count($ValidationErrors) > 0 ) { 
				foreach ($ValidationErrors as $key => $value) { 
					?>
					$("#<?php echo $value[0]?>").css('color', 'red');
				<?php } 
			}
		}
	?>
});
</script>
</head>

<body>

	<div id="header">
		
		<div class="pageTitleText">Add Patient</div>
		
	</div>
    
    <div id="mainMenu" style="height: 65px; width: 100px;">
    	<ul style="list-style: none outside none;">
	        <li>
	       	 	<a href="home.php">Cancel</a>
	        </li> 
	         
	        <li>
	       	 	<a href="logout.php">Logout</a>
	        </li>
	        
        </ul>
</div>
    <div class="formContainer">
		    <form name="editPatientForm" method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
		    
		    	<input type="hidden" name="isPostBack" value="true" />
		    	<!-- <div id="title">
			        <h1>Add Patient</h1>-->
			        
			         <?php if(isset($ValidationErrors)) { ?>
				    	<div class="noQueryResults" style="text-align: left; left: 0px; top: 5px;">
								<img src="../Images/error.png" style="float:left; margin: 5px 5px 5px 15px;"></img>
								<p style="font-weight: bold; line-height: 1; margin: 5px 5px 5px 55px;">Error</p>
								<p style="line-height: 1; margin: 5px 5px 5px 55px;">
								<?php if (count($ValidationErrors) > 1) { 
						    		echo 'Multiple Errors';
						    	} else { 
						    		echo $ValidationErrors[0][1];
						    	}?></p>
						</div>
				    <?php } ?>
			        
			        
			            
			        <div class="Questions" style="width: 500px;">
			        
				         <div id="addPatientContentsdiv">
				       		<h3>
					       		<span id="patientIDdivId">Patient ID</span>: <input type="text" name="patientIDField" class="textInput" value="<?php if (isset($_REQUEST['patientIDField'])) echo $_REQUEST['patientIDField']; ?>" style="width: 100px; text-align:center; margin-left:5px; margin-right:5px;"> 
					       	    <span id="diagnoseIddivId">Date Diagnosed</span>: <input type="text" id="diagnoseDate" name="patientDiagnoseDate" value="<?php if (isset($_REQUEST['isPostBack'])) {echo $_REQUEST['patientDiagnoseDate'];}?>" style="width: 115px;"/>
				       		</h3>
				    	</div> 
				        
				            <div id="basicInfoTable">
					             <table id="table1" border="1">
						                <tr>
						                	<h4>Basic Information</h4>
						                </tr>
						                <tr>
						                	<th><span id="dobSpanId">Date of Birth</span></th>
						                    <td>
											<input type="text" id="patDOB" name="patientDOB" value="<?php if (isset($_REQUEST['isPostBack'])) echo $_REQUEST['patientDOB']; else echo '1911-01-01';?>" />
						                    </td>
						                </tr>
						                <tr>
						                	<th>Gender</th>
						                    <td>
						                    <select name="patientGender" class="textInput">
						                    <option value="M"<?php if($_REQUEST['patientGender'] == 'M') echo 'selected="selected"';?>>M</option>
						                    <option value="F"<?php if($_REQUEST['patientGender'] == 'F') echo 'selected="selected"';?>>F</option>
						                    </select>
						                    </td>
						                </tr>
						                <tr>
						                	<th>Relationship Status</th>
						                    <td>
						                    <select name="patientRelations" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientRelations'] == 0) echo 'selected="selected"';?>>Single</option>
						                    <option value="1"<?php if($_REQUEST['patientRelations'] == 1) echo 'selected="selected"';?>>In a Relationship</option>
						                    </select>
											</td>
						                </tr>
						                <tr>
						                	<th>Occupation Status</th>
						                    <td>
						                    <select name="patientOccupation" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientOccupation'] == 0)  echo 'selected="selected"';?>>Unemployed</option>
						                    <option value="1"<?php if($_REQUEST['patientOccupation'] == 1)  echo 'selected="selected"';?>>Employed</option>
						                    </select>
											</td>
						                </tr>
						                <tr>
						                	<th>Ticks in Work Environment</th>
						                    <td>
											<select name="patientWorkEnv" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientWorkEnv'] == 0) echo 'selected="selected"';?>>No</option>
						                    <option value="1"<?php if($_REQUEST['patientWorkEnv'] == 1) echo 'selected="selected"';?>>Yes</option>
						                    </select>
											</td>
						                </tr>
						                <tr>
						                	<th>Ticks in Hunting Environment</th>
						                    <td>
											<select name="patientHuntEnv" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientHuntEnv'] == 0) echo 'selected="selected"';?>>No</option>
						                    <option value="1"<?php if($_REQUEST['patientHuntEnv'] == 1) echo 'selected="selected"';?>>Yes</option>
						                    </select>
						                    </td>
						                </tr>
						                 <tr>
						                	<th>Ticks in Play Environment</th>
						                    <td>
											<select name="patientPlayEnv" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientPlayEnv'] == 0) echo 'selected="selected"';?>>No</option>
						                    <option value="1"<?php if($_REQUEST['patientPlayEnv'] == 1) echo 'selected="selected"';?>>Yes</option>
						                    </select>
						                    </td>
						                </tr>
						                <tr>
						                	<th>Ticks in Garden Environment</th>
						                    <td>
											<select name="patientGardenEnv" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientGardenEnv'] == 0) echo 'selected="selected"';?>>No</option>
						                    <option value="1"<?php if($_REQUEST['patientGardenEnv'] == 1) echo 'selected="selected"';?>>Yes</option>
						                    </select>
											</td>
						                </tr>
						                <tr>
						                	<th>Tickbite Caused Disease</th>
						                    <td>
											<select name="patientTick" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientTick'] == 0) echo 'selected="selected"';?>>No</option>
						                    <option value="1"<?php if($_REQUEST['patientTick'] == 1) echo 'selected="selected"';?>>Yes</option>
						                    </select>
											</td>
						                </tr>
						                 <tr>
						                	<th>Currently Has a Rash</th>
						                    <td>
											<select name="patientRash" class="textInput">
						                    <option value="0"<?php if($_REQUEST['patientRash'] == 0) echo 'selected="selected"';?>>No</option>
						                    <option value="1"<?php if($_REQUEST['patientRash'] == 1) echo 'selected="selected"';?>>Yes</option>
						                    </select>
											</td>
						                </tr>
					                </table>
				            	</div>
		   		</div>
		   		 <input type="submit" value="Submit" class="loginButton" style="margin-top: 0px;"/> <!-- id="finishButton" -->
		
		    </form>
	</div>    
</body>
</html>